package com.des1gn.util;

import com.aspose.cells.Workbook;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Objects;

/**
 * @ClassName WordToPdfUtil
 * @Description TODO
 * @Author Des1gn
 * @Date 2021/7/5 16:23
 * @Version 1.0
 **/
public class WordToPdfUtil {
  private static  final  String PDF=".pdf";
    public static boolean getLicense() {
        boolean result = false;
        try {
            // license.xml应放在..\WebRoot\WEB-INF\classes路径下
            InputStream is = WordToPdfUtil.class.getClassLoader().getResourceAsStream("license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String doc2pdf(String inPath, String outPath) {
        if (!getLicense()) {
            // 验证License 若不验证则转化出的pdf文档会有水印产生
            return null;
        }
        try {
            outPath = String.format("%s%s%s", outPath, UuidUtil.getUUid(), PDF);
            File file = new File(outPath);
            // 新建一个空白pdf文档
            FileOutputStream os = new FileOutputStream(file);
            Document doc = new Document(inPath);
            // Address是将要被转化的word文档
            doc.save(os, SaveFormat.PDF);
            // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
            os.close();
            return outPath;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String doc2pdf(InputStream inputStream, String outPath) {
        if (!getLicense()) {
            // 验证License 若不验证则转化出的pdf文档会有水印产生
            return null;
        }
        try {
            outPath = String.format("%s%s%s", new Object[]{outPath, UuidUtil.getUUid(), PDF});
            File file = new File(outPath);
            // 新建一个空白pdf文档
            FileOutputStream os = new FileOutputStream(file);
            Document doc = new Document(inputStream);
            // Address是将要被转化的word文档
            doc.save(os, SaveFormat.PDF);
            // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
            os.close();
            return outPath;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * @Description TODO
     * @param inputPath 需要被转换的excel全路径带文件名
     * @param outPath 转换之后pdf的全路径带文件名
     * @Return void
     * @Author Mr.Walloce
     * @Date 2019/7/27 13:48
     */
    public static void excelToPdf(String inputPath, String outPath) {
        if (!getLicense()) {
            return;
        }
        try {
            //Excel文件数据
            Workbook wb = new Workbook(inputPath);
            FileOutputStream fileOS = new FileOutputStream(new File(outPath));
            //保存为pdf文件，saveFormat取的是cells包下的，值为：13
            wb.save(fileOS, com.aspose.cells.SaveFormat.PDF);
            fileOS.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *  待转换图片路径 ， pdf路径 ,
     * @param byteArray
     * @param outputFile
     * @throws IOException
     */
    public static String imgToPDF(byte[] byteArray, String outputFile) throws IOException {
        if (!outputFile.toLowerCase().endsWith(PDF)) {
            outputFile = String.format("%s%s%s", new Object[]{outputFile, UuidUtil.getUUid(), PDF});
        }
        InputStream inputStream = new ByteArrayInputStream(byteArray);
        BufferedImage bimg = ImageIO.read(inputStream);
        if (bimg == null){
            inputStream.close();
            throw new RuntimeException("src file invalid");
        }
        float width = bimg.getWidth();
        float height = bimg.getHeight();
        PDDocument document = new PDDocument();
        try {
            PDPage page = new PDPage(new PDRectangle(width, height));
            document.addPage(page);
            PDImageXObject img = PDImageXObject.createFromByteArray(document,byteArray,"pdf");
            PDPageContentStream contentStream = new PDPageContentStream(document, page);
            try {
                contentStream.drawImage(img, 0, 0, width, height);
            }finally {
                contentStream.close();
            }
            document.save(outputFile);
            return outputFile;
        }finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                document.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
